/*
* 时间设置
* 舞者无罪
* 2014.12.01
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <mh/common.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "yarn_check.h"
#include "com.h"


#define YARN_CHECK_CAPTION_TOP        (CY_TITLE4_HEIGHT + CX_EDGE_LEFT * 2)
#define YARN_CHECK_CAPTION_WIDTH      (40)
#define YARN_CHECK_CAPTION_HEIGHT     (28)
#define YARN_CHECK_INTER_CX           (CX_YARN_CHECK_BMP / 2)
#define YARN_CHECK_CAPTION_LEFT       ((CX_YARN_CHECK_BMP / 2 - YARN_CHECK_CAPTION_WIDTH * 2) / 2)
/*
 *
 */
static void CreateYarnCheckInterface(HWND hWnd)
{
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHECK_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHECK_BMP - CX_ASK_BMP * 3 - 8,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHECK_BMP - CX_ASK_BMP * 4 - 8 * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_ASK]);

    CreateWindow (MH_LABEL_NAME,
        "L:",
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHECK_CAPTION_LEFT,
        YARN_CHECK_CAPTION_TOP,
        YARN_CHECK_CAPTION_WIDTH,
        YARN_CHECK_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "R:",
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHECK_CAPTION_LEFT + YARN_CHECK_INTER_CX,
        YARN_CHECK_CAPTION_TOP,
        YARN_CHECK_CAPTION_WIDTH,
        YARN_CHECK_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_ESC:
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
        break;
    default:
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
}
/*
 *
 */
static int YarnCheckWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateYarnCheckInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_CYAN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            rect.top = YARN_CHECK_CAPTION_TOP + YARN_CHECK_CAPTION_HEIGHT * 2;
            rect.bottom = rect.top + CY_YARN_USED_BMP;
            int i = 0;
            SetTextColor(hdc, COLOR_HQ_BLACK(hdc));
            char *strNum[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
            uchar pdrt[16] = {0};
            KNIT_PAGE *pcon = (KNIT_PAGE*)GetControlData(g_knit_data);
            if(pcon)
            {
                GetAllYarnStartDrt(g_knit_data, pcon, 1, GetTotalKnitPage(g_knit_data), pdrt);
            }
            for(i = 0; i < 8; ++i)
            {
                uchar used = 0;
                used |= pdrt[i];
                used |= pdrt[i + 8];
                rect.left = CX_EDGE_LEFT * 4 + i * CX_YARN_USED_BMP;
                rect.right = rect.left + CX_YARN_USED_BMP;
                FillBoxWithBitmap (hdc, rect.left, rect.top, CX_YARN_USED_BMP,
                    CY_YARN_USED_BMP, &g_bmp[used & 1 ? BMP_YARN_USED : BMP_YARN_NO_USED]);
                DrawText(hdc, strNum[i], -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
                rect.left += CX_YARN_CHECK_BMP / 2;
                rect.right = rect.left + CX_YARN_USED_BMP;
                FillBoxWithBitmap (hdc, rect.left, rect.top, CX_YARN_USED_BMP,
                    CY_YARN_USED_BMP, &g_bmp[used & 2 ? BMP_YARN_USED : BMP_YARN_NO_USED]);
                DrawText(hdc, strNum[i], -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            }
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_YARN_CHECK]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
*
*/
HWND CreateYarnCheckWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_YARN_CHECK_BMP) / 2;
    int y = (CY_SCREEN - CY_YARN_CHECK_BMP) / 2;

    hHosting = GetMainWindowHandle (hHosting);

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_YARN_CHECK_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = YarnCheckWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_YARN_CHECK_BMP;
    CreateInfo.by = y + CY_YARN_CHECK_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow (hHosting, FALSE);
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
